/**
 * 作业监督进行getById与getList的操作
 * 查看需要返回作业票表（基本信息、风险分析）、审批记录表、延期审批记录表、作业检查记录小表、作业监督记录小表、作业终止记录小表（保留class）
 */
package com.xidian.zhiancloud.controller.dangerWorkTicket;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.workSupervise.ReviewWorkBefore;
import com.xidian.zhiancloud.model.workSupervise.SuperviseWork;
import com.xidian.zhiancloud.model.workSupervise.SuspendWork;
import com.xidian.zhiancloud.model.dangerWorkTicket.WorkTicket;
import com.xidian.zhiancloud.qo.dangerWorkTicket.WorkTicketQO;
import com.xidian.zhiancloud.service.dangerWorkTicket.ReviewWorkBeforeService;
import com.xidian.zhiancloud.service.dangerWorkTicket.SuperviseWorkService;
import com.xidian.zhiancloud.service.dangerWorkTicket.SuspendWorkService;
import com.xidian.zhiancloud.service.dangerWorkTicket.WorkSuperviseService;
import com.xidian.zhiancloud.service.dangerWorkTicket.WorkTicket.WorkTicketService;
import com.xidian.zhiancloud.vo.dangerWorkTicket.workSupervise.WorkSuperviseDTOVO;
import com.xidian.zhiancloud.vo.dangerWorkTicket.WorkTicketVO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@RestController
@RequestMapping("/WorkSupervise")
public class WorkSuperviseController extends BaseController {
    @Autowired
    private WorkSuperviseService workSuperviseService;

    @Autowired
    private ReviewWorkBeforeService reviewWorkBeforeService;

    @Autowired
    private SuperviseWorkService superviseWorkService;

    @Autowired
    private SuspendWorkService suspendWorkService;

    @Autowired
    private WorkTicketService workTicketService;

    /**
     * 根据id查找
     *
     * @param id
     * @return
     */
    @GetMapping("/getById")
    public ResultDTO<WorkSuperviseDTOVO> getVOById(@RequestParam("id") Long id) {
        return ResultDTO.successOf(workSuperviseService.getVOById(id), GeneralResponseEnums.SUCCESS);
    }


    /**
     * 列表查询
     *
     * @param workTicketQO
     * @return
     */
    @GetMapping("/getListByQO")
    public ResultDTO<IPage<WorkTicketVO>> getListByQO(WorkTicketQO workTicketQO) {
        if(workTicketQO.getCurrent() == null || workTicketQO.getSize() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        workTicketQO.setCreatorId(getSessionUserid());
        workTicketQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(workSuperviseService.getListByQO(workTicketQO), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 1. "【作业票管理】-【新增】-【选择监护人a】" —— "【a的作业监督】-【查看，仅显示作业基本信息】"
     * 2. "【作业票管理】-【提交】" —— "【a的作业审批】-【审批】-【通过&同意】（提交后显示）" —— "【的作业监督，查看基本信息】"
     * 3. “【审批同意】-【a的作业监督】-【作业前检查】-【显示基本信息 & 作业前检查记录 & 风险分析及主要安全措施评定（必须） & 上传附件（非必须）】”
     * 4. "【作业审批】-【作业开始】-【添加监督记录（必须上传附件）（可多次）】-【作业中止（添加记录）】-【查看】-【作业恢复】-【查看】-【作业结束】-【查看】" —— "【作业台账（仅显示结束的作业）】-【导出】"
     * 5. "【作业管理】-【申请延期】" —— "【我的延期申请】-【查看】" —— "【作业审批】-【延期申请审批】-【查看】-【同意】-（猜测）【改变作业监督-预计作业结束时间】"
     */


    /**
     * 作业前检查。作业前检查。对风险分析及主要安全措施进行判断。每进行一次检查，则t_review_work_before增加一条记录，relate_work_id由前端给出
     *
     * @param reviewWorkBefore
     * @return
     */
    @PostMapping("/ReviewWorkBefore")
    public ResultDTO<Boolean> beforeReview(ReviewWorkBefore reviewWorkBefore) {
        reviewWorkBefore.setCompanyId(getSessionUserCompanyId());
        reviewWorkBefore.setCheckTime(new Date(System.currentTimeMillis()));
        reviewWorkBefore.setCheckerId(getSessionUserid());
        // 默认为1符合，如果有0，则设为不符合
        if( reviewWorkBefore.getOpinion().contains("0") ){
            reviewWorkBefore.setCheckResult(0);
        } else {
            reviewWorkBefore.setCheckResult(1);
        }
        Boolean res = reviewWorkBeforeService.save(reviewWorkBefore);

        return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 作业开始
     *
     * @param id
     * @return
     */
    @PostMapping("/startWork")
    public ResultDTO<Boolean> startWork(@RequestParam("id") Long id) {
        WorkTicket workTicket = workTicketService.getById(id);
        if (workTicket == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        // 新建作业票，为作业进行中
        workTicket.setWorkStatus(3);
        workTicket.setRealStartTime(new Date(System.currentTimeMillis()));
        Boolean res = workTicketService.updateById(workTicket);
        return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
    }


    /**
     * 添加监督记录
     *
     * @param superviseWork
     * @return
     */
    @PostMapping("/addSuperviseRecord")
    public ResultDTO<Boolean> superviseRecord(SuperviseWork superviseWork) {
        superviseWork.setCompanyId(getSessionUserCompanyId());
        Boolean res =superviseWorkService.save(superviseWork);
        return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 添加作业中止记录
     *
     * @param suspendWork
     * @return
     */
    @PostMapping("/addSuspendRecord")
    public ResultDTO<Boolean> addSuspendRecord(SuspendWork suspendWork) {
        suspendWork.setCompanyId(getSessionUserCompanyId());
        Boolean res = suspendWorkService.save(suspendWork);
        WorkTicket workTicket = workTicketService.getById(suspendWork.getRelateWorkId());
        workTicket.setSuspendRecoverId(suspendWork.getId());
        workTicket.setWorkStatus(4);
        workTicketService.updateById(workTicket);

        return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 添加作业恢复记录
     *
     * @param relateWorkId
     * @param recoverTime
     * @return
     */
    @PostMapping("/addRecoverRecord")
    public ResultDTO<Boolean> addRecoverRecord(@RequestParam("relateWorkId") Long relateWorkId, @RequestParam("recoverTime") Date recoverTime) {
        Long id = workTicketService.getById(relateWorkId).getSuspendRecoverId();
        SuspendWork record = suspendWorkService.getById(id);
        record.setRecoverTime(new Date(System.currentTimeMillis()));
        record.setRecoverId(getSessionUserid());
        Boolean res = suspendWorkService.updateById(record);

        // 更改作业票状态
        WorkTicket workTicket = workTicketService.getById(id);
        if (workTicket == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        // 新建作业票，为作业进行中
        workTicket.setWorkStatus(3);
        workTicketService.updateById(workTicket);

        return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
    }


    /**
     * 作业前检查记录详情
     *
     * @param id t_review_work_before的id
     * @return
     */
    @GetMapping("/reviewWorkDetails")
    public ResultDTO<Map<String, Integer>> beforeReviewDetails(@RequestParam("id") Long id) {
         ReviewWorkBefore reviewWorkBefore = reviewWorkBeforeService.getById(id);
         if (reviewWorkBefore == null) {
             return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
         } else {
             // 返回Map
             Map<String, Integer> riskAssess = reviewWorkBeforeService.getRiskAssess(reviewWorkBefore);
             return ResultDTO.successOf(riskAssess, GeneralResponseEnums.SUCCESS);
         }
    }



    /**
     * 将相关字段设置为“作业结束状态”
     * @param id
     * @return
     */
    @PostMapping("/endWork")
    public ResultDTO<Boolean> endWork(@RequestParam("id") Long id) {
        WorkTicket workTicket = workTicketService.getById(id);
        if (workTicket == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        workTicket.setWorkStatus(5);
        workTicket.setRealEndTime(new Date(System.currentTimeMillis()));
        Boolean res = workTicketService.updateById(workTicket);
        return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
    }

}
